package com.itheima.travel.service.impl;

import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.AffixMapper;
import com.itheima.travel.pojo.Affix;
import com.itheima.travel.pojo.AffixExample;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Description：文件上传实现
 */
@Service
public class AffixServiceImpl implements AffixService {

    @Autowired
    AffixMapper affixMapper;

    @Autowired
    private RedissonClient redissonClient;
    //@Autowired
    //SnowflakeIdWorker snowflakeIdWorker;

    // 文件上传的位置
    @Value("${upLoad.pathRoot}")
    String pathRoot;

    @Override
    public AffixVo upLoad(MultipartFile multipartFile,
                          AffixVo affixVo) throws IOException {
        //判断文件是否为空
        if (EmptyUtil.isNullOrEmpty(multipartFile)) {
            return null;
        }
        // 获取图片类型(大图/小图)
        String businessType = affixVo.getBusinessType();

        //原始上传的文件名称aaa.jpg
        String originalFilename = multipartFile.getOriginalFilename();
        //后缀名称.jpg
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        affixVo.setSuffix(suffix);
        //文件名称
        //String fileName = String.valueOf(snowflakeIdWorker.nextId());
        String fileName = String.valueOf(UUID.randomUUID().toString());
        affixVo.setFileName(fileName);
        //构建访问路径
        String pathUrl = businessType + "/" + fileName + suffix;
        //判断业务类型的文件夹是否存在
        File file = new File(pathRoot + businessType);
        //文件夹不存在则创建
        if (!file.exists()) {
            file.mkdir();
        }
        file = new File(pathRoot + "/" + pathUrl);
        multipartFile.transferTo(file);
        affixVo.setPathUrl(pathUrl);
        Affix affix = BeanConv.toBean(affixVo, Affix.class);
        affixMapper.insert(affix);
        return BeanConv.toBean(affix, AffixVo.class);
    }

    @Override
    public Boolean bindBusinessId(AffixVo affixVo) {
        Affix affix = BeanConv.toBean(affixVo, Affix.class);
        int flag = affixMapper.updateByPrimaryKeySelective(affix);
        return flag > 0;
    }

    @Override
    public List<AffixVo> findAffixByBusinessId(AffixVo affixVo) {
        // 先查询redis
        String key = RedisConstant.AFFIXSERVICE_FINDAFFIXBYBUSINESSID + affixVo.getBusinessId();
        // 获取缓存数据的桶位(获取数据在redis内存中的位置)
        RBucket<List<AffixVo>> bucket = redissonClient.getBucket(key);
        // 从桶位中获取数据
        List<AffixVo> affixVoList = bucket.get();
        if (EmptyUtil.isNullOrEmpty(affixVoList)) {
            // 缓存中没有查询到数据
            // 查询mysql
            AffixExample example = new AffixExample();
            example.createCriteria().andBusinessIdEqualTo(affixVo.getBusinessId());
            List<Affix> affixes = affixMapper.selectByExample(example);
            affixVoList = BeanConv.toBeanList(affixes, AffixVo.class);
            if (!EmptyUtil.isNullOrEmpty(affixVoList)) {
                // 将从mysql中查询到的数据缓存到redis中
                bucket.set(affixVoList, 1800, TimeUnit.SECONDS);
            }
        }

        return affixVoList;
    }
}
